package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcCsunDoorLocationMapper;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.DmcCsunButtonLocation;
import com.csun.cmny.provider.model.domain.DmcCsunDoorLocation;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.dto.button.BindCsunButtonLocationDto;
import com.csun.cmny.provider.model.dto.button.CsunButtonPageQuery;
import com.csun.cmny.provider.model.dto.csundoor.BindCsunDoorLocationDto;
import com.csun.cmny.provider.model.dto.csundoor.CsunDoorPageQuery;
import com.csun.cmny.provider.model.dto.propertyManagement.SelectDeviceDataDto;
import com.csun.cmny.provider.model.enums.button.DmcCsunButtonBindTypeEnum;
import com.csun.cmny.provider.model.enums.device.DmcDeviceTypeEnum;
import com.csun.cmny.provider.model.vo.OldmanBedRoomBuildingVo;
import com.csun.cmny.provider.model.vo.csunbutton.DmcCsunButtonVo;
import com.csun.cmny.provider.model.vo.csundoor.DmcCsunDoorVo;
import com.csun.cmny.provider.model.vo.propertyManagement.PropertyDataVo;
import com.csun.cmny.provider.service.DmcCsunDoorLocationService;
import com.csun.cmny.provider.service.DmcDeviceAlertService;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.util.PubUtils;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2020-06-18 10:10
 * @Description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DmcCsunDoorLocationServiceImpl extends BaseService<DmcCsunDoorLocation> implements DmcCsunDoorLocationService {

    @Resource
    private DmcCsunDoorLocationMapper dmcCsunDoorLocationMapper;
    @Resource
    private DmcDeviceService dmcDeviceService;
    @Resource
    private DmcDeviceAlertService dmcDeviceAlertService;

    /**
     * bind csun door location
     *
     * @param bindCsunDoorLocationDto bind csun door location dto
     * @param loginAuthDto            login auth dto
     * @return the int.
     */
    @Override
    public Integer bindCsunDoorLocation(BindCsunDoorLocationDto bindCsunDoorLocationDto, LoginAuthDto loginAuthDto) {

        // 1.校验智能床垫 - 日创
        DmcDevice dmcDevice = dmcDeviceService
                .checkDeviceIsCsunButtonByDeviceId(bindCsunDoorLocationDto.getDeviceId());
        if (PublicUtil.isEmpty(dmcDevice)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610073);
        }

        // 2.校验紧急按钮是否已经被绑定
        DmcCsunDoorLocation dmcCsunDoorLocationQuery = dmcCsunDoorLocationMapper
                .selectByPrimaryKey(bindCsunDoorLocationDto.getDeviceId());
        if (PublicUtil.isNotEmpty(dmcCsunDoorLocationQuery)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610074);
        }

        // TODO 按钮绑定老人、床位的关系 1:1:1

        // 3.校验绑定类型
//        Preconditions.checkArgument(!PubUtils.isNull(bindCsunButtonLocationDto.getBindType()),
//                cmnyMessageSource.getMessage("csun.button.location.bind.type.null", null));
//
//        Preconditions.checkArgument(bindCsunButtonLocationDto.getBindType() >= 0
//                        && bindCsunButtonLocationDto.getBindType() <= 2,
//                cmnyMessageSource.getMessage("csun.button.location.bind.type.range", null));
//
//        // 4.校验位置
//        if (bindCsunButtonLocationDto.getBindType().equals(DmcCsunButtonBindTypeEnum.CUSTOM.getKey())) {
//            Preconditions.checkArgument(!PubUtils.isNull(bindCsunButtonLocationDto.getLocation()),
//                    cmnyMessageSource.getMessage("csun.button.location.null", null));
//        }
//
//        if (bindCsunButtonLocationDto.getBindType().equals(DmcCsunButtonBindTypeEnum.OLDMAN.getKey())) {
//            // 校验老人Id和老人姓名
//            Preconditions.checkArgument(!PubUtils.isNull(bindCsunButtonLocationDto.getOldmanId()),
//                    cmnyMessageSource.getMessage("csun.button.location.oldman.id.null", null));
//
//            Preconditions.checkArgument(!PubUtils.isNull(bindCsunButtonLocationDto.getOldmanName()),
//                    cmnyMessageSource.getMessage("csun.button.location.oldman.name.null", null));
//            // TODO 校验老人Id是否存在
//            OldmanBedRoomBuildingVo oldmanBedRoomBuildingVo = omcRpcService
//                    .queryOldmanBedRoomBuildingByOldmanId(bindCsunButtonLocationDto.getOldmanId());
//            if (PublicUtil.isEmpty(oldmanBedRoomBuildingVo)) {
//                // 老人不存在
//                throw new DmcBizException(ErrorCodeEnum.DMC100610062);
//            }
//
//            /**
//             * @date    2019-05-22
//             * @author  chisj
//             * @desc    校验老人是否已经绑定过紧急按钮
//             */
////            dmcCsunButtonLocationQuery = new DmcCsunButtonLocation();
////            dmcCsunButtonLocationQuery.setBindType(DmcCsunButtonBindTypeEnum.OLDMAN.getKey());
////            dmcCsunButtonLocationQuery.setOldmanId(bindCsunButtonLocationDto.getOldmanId());
////            int count = dmcCsunButtonLocationMapper.selectCount(dmcCsunButtonLocationQuery);
////            if (count > 0) {
////                // 老人已经绑定过紧急按钮
////                throw new DmcBizException(ErrorCodeEnum.DMC100610064);
////            }
////
//            bindCsunButtonLocationDto.setOldmanName(oldmanBedRoomBuildingVo.getOldmanName());
//            if (PublicUtil.isNotEmpty(oldmanBedRoomBuildingVo.getBedId())) {
//                bindCsunButtonLocationDto.setBedId(oldmanBedRoomBuildingVo.getBedId());
//                bindCsunButtonLocationDto.setLocation(oldmanBedRoomBuildingVo.getLocation());
//            }
//
//            /**
//             * @date    2019-11-18
//             * @author  chisj
//             * @desc    老人可以进行一对多绑定紧急按钮
//             */
//        }
//
//        // 5.校验床位Id
//        if (bindCsunButtonLocationDto.getBindType().equals(DmcCsunButtonBindTypeEnum.BED.getKey())) {
//            // 绑定类型为房间
//            Preconditions.checkArgument(!PubUtils.isNull(bindCsunButtonLocationDto.getBedId()),
//                    cmnyMessageSource.getMessage("csun.button.location.bed.id.null", null));
//            // 校验床位Id是否存在
//            // TODO 校验床位Id是否存在
//            OldmanBedRoomBuildingVo bedRoomBuildingVo = omcRpcService
//                    .queryOldmanBedRoomBuildingByBedId(bindCsunButtonLocationDto.getBedId());
//            if (PublicUtil.isEmpty(bedRoomBuildingVo)) {
//                // 床位不存在
//                throw new DmcBizException(ErrorCodeEnum.DMC100610060);
//            }
//
//            /**
//             * @date    2019-05-22
//             * @author  chisj
//             * @desc    校验床位是否绑定过紧急按钮
//             */
////            dmcCsunButtonLocationQuery = new DmcCsunButtonLocation();
////            dmcCsunButtonLocationQuery.setBindType(DmcCsunButtonBindTypeEnum.BED.getKey());
////            dmcCsunButtonLocationQuery.setBedId(bindCsunButtonLocationDto.getBedId());
////            int count = dmcCsunButtonLocationMapper.selectCount(dmcCsunButtonLocationQuery);
////            if (count > 0) {
////                // 老人已经绑定过紧急按钮
////                throw new DmcBizException(ErrorCodeEnum.DMC100610065);
////            }
////
////            bindCsunButtonLocationDto.setOldmanId(bedRoomBuildingVo.getOldmanId());
////            bindCsunButtonLocationDto.setOldmanName(bedRoomBuildingVo.getOldmanName());
////            bindCsunButtonLocationDto.setLocation(bedRoomBuildingVo.getLocation());
//            /**
//             * @date    2019-11-18
//             * @author  chisj
//             * @desc    床位可以进行一对多绑定紧急按钮
//             */
//        }

        // 6.添加绑定
        DmcCsunDoorLocation dmcCsunDoorLocation = new DmcCsunDoorLocation();
        BeanUtils.copyProperties(bindCsunDoorLocationDto, dmcCsunDoorLocation);
        dmcCsunDoorLocation.setUpdateInfo(loginAuthDto);

        return dmcCsunDoorLocationMapper.insertSelective(dmcCsunDoorLocation);
    }

    @Override
    public List<PropertyDataVo> selectDeviceData(SelectDeviceDataDto selectDeviceDataDto) {
        return dmcCsunDoorLocationMapper.selectDeviceData(selectDeviceDataDto);
    }

    /**
     * break csun button location
     *
     * @param deviceId device id
     * @return the int.
     */
    @Override
    public Integer breakCsunDoorLocation(Long deviceId) {

        // 1.判断设备Id是否存在
        DmcDevice dmcDevice = dmcDeviceService.checkDeviceIsCsunButtonByDeviceId(deviceId);
        if (PublicUtil.isEmpty(dmcDevice)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610073);
        }

        // 2.判断紧急按钮是否已经绑定
        DmcCsunDoorLocation dmcCsunDoorLocationQuery = dmcCsunDoorLocationMapper
                .selectByPrimaryKey(deviceId);
        if (PublicUtil.isEmpty(dmcCsunDoorLocationQuery)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610032, deviceId);
        }

        // 3.解绑紧急按钮
        Integer result = dmcCsunDoorLocationMapper.deleteByPrimaryKey(deviceId);
        if (result > 0) {
            dmcDeviceAlertService.deleteByDeviceId(deviceId);
        }

        return result;
    }

    /**
     * query csun door list with page
     *
     * @param csunDoorPageQuery csun door page query
     * @return thee info.
     */
    @Override
    public PageInfo queryGroupCsunDoorListWithPage(CsunDoorPageQuery csunDoorPageQuery) {

        PageHelper.startPage(csunDoorPageQuery.getPageNum(), csunDoorPageQuery.getPageSize());

        List<DmcCsunDoorVo> dmcCsunButtonVoList = dmcCsunDoorLocationMapper
                .queryGroupCsunDoorListWithPage(csunDoorPageQuery);

        return new PageInfo<>(dmcCsunButtonVoList);
    }

    /**
     * get csun door by device id
     *
     * @param deviceId device id
     * @return dmc csun door vo
     */
    @Override
    public DmcCsunDoorVo getCsunDoorByDeviceId(Long deviceId) {

        CsunDoorPageQuery csunDoorPageQuery = new CsunDoorPageQuery();
        csunDoorPageQuery.setDeviceId(deviceId);
        csunDoorPageQuery.setDeviceType(DmcDeviceTypeEnum.CSUN_BUTTON.getIndex());

        PageHelper.startPage(1, 1);

        List<DmcCsunDoorVo> dmcCsunDoorVoList = dmcCsunDoorLocationMapper
                .queryGroupCsunDoorListWithPage(csunDoorPageQuery);

        if (PubUtils.isNull(dmcCsunDoorVoList)) {
            return null;
        }

        return dmcCsunDoorVoList.get(0);
    }

    /**
     * get csun door by device id
     *
     * @param deviceNo device no
     * @return dmc csun door vo
     */
    @Override
    public DmcCsunDoorVo getCsunDoorByDeviceNo(String deviceNo) {

        CsunDoorPageQuery csunDoorPageQuery = new CsunDoorPageQuery();
        csunDoorPageQuery.setDeviceNo(deviceNo);
        csunDoorPageQuery.setDeviceType(DmcDeviceTypeEnum.CSUN_DOOR.getIndex());

        PageHelper.startPage(1, 1);

        List<DmcCsunDoorVo> dmcCsunDoorVoList = dmcCsunDoorLocationMapper
                .queryGroupCsunDoorListWithPage(csunDoorPageQuery);

        if (PubUtils.isNull(dmcCsunDoorVoList)) {
            return null;
        }

        return dmcCsunDoorVoList.get(0);
    }

    /**
     * delete csun door by id
     *
     * @param id the id
     * @return the int.
     */
    @Override
    public Integer deleteCsunDoorById(Long id) {

        // 1.判断设备Id是否存在
        DmcDevice dmcDevice = dmcDeviceService.checkDeviceIsCsunButtonByDeviceId(id);
        if (PublicUtil.isEmpty(dmcDevice)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610073, id);
        }

        // 2.判断紧急按钮是否已经绑定
        DmcCsunDoorLocation dmcCsunDoorLocationQuery = dmcCsunDoorLocationMapper
                .selectByPrimaryKey(id);
        if (PublicUtil.isNotEmpty(dmcCsunDoorLocationQuery)) {
            dmcCsunDoorLocationMapper.deleteByPrimaryKey(id);
        }

        // 3.删除设备
        return dmcDeviceService.deleteDmcDeviceById(id);
    }

}
